﻿namespace Application.Utils
{
    //public static class DistributedLockManager
    //{
    //    private static object lockObj = new object();
    //    public static bool Lock(JobManagerDbContext dbContext, Resource resource, Action action)
    //    {
    //        lock (lockObj)
    //        {
    //            try
    //            {
    //                var id = (int)resource;
    //                using (var transaction = dbContext.Database.BeginTransaction())
    //                {
    //                    var lockInfo = dbContext.DistributedLocks.FirstOrDefault(s => s.Id == id);
    //                    if (lockInfo == null) dbContext.Database.ExecuteSqlRaw($"insert into job_distributedlocks(id,AcquiredAt) values({id},'{DateTime.Now.DateTime()}')");

    //                    var res = dbContext.Database.ExecuteSqlRaw($"SELECT * FROM job_distributedlocks WHERE id = {id} FOR UPDATE nowait;");
    //                    action.Invoke();
    //                    transaction.Commit();
    //                    return true;
    //                }
    //            }
    //            catch
    //            {
    //                return false;
    //            }
    //        }
    //    }

    //}

}
